package models

import (
	"database/sql/driver"
	"time"

	toolsdatatype "git.querycap.com/tools/datatypes"
	"github.com/go-courier/sqlx/v2"
	"github.com/go-courier/sqlx/v2/datatypes"
)

var DBVideoHub = sqlx.NewDatabase("video_hub")

type OperationTimes struct {
	// 创建时间
	CreatedAt Timestamp `db:"f_created_at,default='0'" json:"createdAt" `
	// 更新时间
	UpdatedAt Timestamp `db:"f_updated_at,default='0'" json:"updatedAt"`
}

func (times *OperationTimes) MarkUpdatedAt() {
	times.UpdatedAt = Timestamp(time.Now())
}

func (times *OperationTimes) MarkCreatedAt() {
	times.MarkUpdatedAt()
	times.CreatedAt = times.UpdatedAt
}

type OperationTimesWithDeletedAt struct {
	OperationTimes
	// 删除时间
	DeletedAt Timestamp `db:"f_deleted_at,default='0'" json:"-"`
}

func (times *OperationTimesWithDeletedAt) MarkDeletedAt() {
	times.MarkUpdatedAt()
	times.DeletedAt = times.UpdatedAt
}

type SFID = toolsdatatype.SFID
type SFIDs = toolsdatatype.SFIDs
type Timestamp = datatypes.MySQLTimestamp
type Bool = datatypes.Bool

var (
	BOOL_TRUE = datatypes.BOOL_TRUE
)

type PrimaryID struct {
	// 自增ID
	ID uint64 `db:"f_id,autoincrement" json:"-"`
}

type Text string

func (Text) DataType(driver string) string {
	return "text"
}

type Meta map[string][]string

func (Meta) DataType(driver string) string {
	return "text"
}

func (p Meta) Value() (driver.Value, error) {
	return datatypes.JSONValue(p)
}

func (p *Meta) Scan(src interface{}) error {
	return datatypes.JSONScan(src, p)
}
